/**
 * KurseViewService.java
 * com.sftj.web.studycenter.kurse.service
 * Copyright (c) 2018, 北京科技有限公司版权所有.
*/

package com.sftz.modules.minapp.kurse.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.SqlManager;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sftz.assets.entities.SeriesCoursesEntity;
import com.sftz.assets.entities.UserCoursesltEntity;
import com.sftz.assets.entities.UserCoursesltInfoEntity;
import com.sftz.assets.enums.OrderTypeEnum;
import com.sftz.modules.minapp.kurse.dto.CategoryDTO;
import com.sftz.modules.minapp.kurse.dto.CourseDTO;
import com.sftz.modules.minapp.kurse.dto.CourseSltDTO;
import com.sftz.modules.minapp.kurse.dto.MykurseDto;
import com.sftz.modules.minapp.kurse.form.KurseForm;
import com.sftz.modules.minapp.user.cache.RedissionUserCache;
import com.uxuexi.core.db.dao.IDbDao;
import com.uxuexi.core.db.util.DbSqlUtil;

/**
 * @author   haozhenbin
 * @Date	 2018年12月11日 	 
 */
@IocBean
public class KurseViewService {
	@Inject
	private IDbDao dbDao;
	@Inject
	private SqlManager sqlManager;

	/**
	 * 系列课程数据
	 */
	public Map<String, Object> listData(Pager pager, KurseForm sqlForm) {
		Map<String, Object> obj = Maps.newHashMap();
		Integer userId = RedissionUserCache.getUserId(sqlForm.getToken());
		Sql kurseSql = Sqls.create(sqlManager.get("minapp_kurse_list"));
		kurseSql.params().set("userId", userId).set("series", OrderTypeEnum.COURSESERIES.intKey());
		List<MykurseDto> kurseList = DbSqlUtil.query(dbDao, MykurseDto.class, kurseSql);
		obj.put("kurseList", kurseList);
		return obj;

	}

	public Map<String, Object> courseSltSub(Integer seriesId, String token) {
		Integer userId = RedissionUserCache.getUserId(token);
		Map<String, Object> map = Maps.newHashMap();
		//获取专业信息
		List<CourseDTO> courseInfo = new ArrayList<CourseDTO>();
		SeriesCoursesEntity seriesEntity = dbDao.fetch(SeriesCoursesEntity.class, Cnd.where("id", "=", seriesId));
		//获取分类列表
		UserCoursesltEntity userCourseslt = dbDao.fetch(UserCoursesltEntity.class, Cnd.where("userId", "=", userId)
				.and("seriesId", "=", seriesId));

		Sql courseGroSql = Sqls.create(sqlManager.get("minapp_courseSlt_groupInfo"));
		courseGroSql.setParam("userCoursesltId", userCourseslt.getId());
		List<UserCoursesltInfoEntity> category = DbSqlUtil.query(dbDao, UserCoursesltInfoEntity.class, courseGroSql);
		List<CategoryDTO> categorys = Lists.transform(category, new Function<UserCoursesltInfoEntity, CategoryDTO>() {
			@Override
			public CategoryDTO apply(UserCoursesltInfoEntity input) {
				// TODO Auto-generated method stub
				CategoryDTO dto = new CategoryDTO();
				dto.setId(input.getClassId());
				dto.setCategoryName(input.getClassName());
				dto.setIsRequired(input.getIsRequired());//存储选修、必修操作
				Sql courseSql = Sqls.create(sqlManager.get("minapp_courseSltSub_infoList"));
				courseSql.setParam("classid", input.getClassId());
				courseSql.setParam("userCoursesltId", userCourseslt.getId());
				List<CourseDTO> course = DbSqlUtil.query(dbDao, CourseDTO.class, courseSql);
				dto.setCourses(course);
				//添加用户选课相关信息--haozhenbin
				Sql courseSltInfo = Sqls.create(sqlManager.get("minapp_courseSlt_info"));
				courseSltInfo.setParam("userCoursesltId", userCourseslt.getId());
				List<CourseSltDTO> CourseSlt = DbSqlUtil.query(dbDao, CourseSltDTO.class, courseSltInfo);
				dto.setCourseSltInfo(CourseSlt);
				//封装选课信息
				for (CourseDTO courseDTO : course) {
					courseInfo.add(courseDTO);
				}
				
				return dto;
			}
		});
		map.put("categorys", categorys);
		map.put("courseInfo", courseInfo);
		map.put("seriesId", seriesId);
		map.put("serName", seriesEntity.getName());
		//查询系列相关的总学分
		map.put("score", seriesEntity.getScore());
		map.put("courseBxScore", userCourseslt.getCourseSltScore());
		return map;
	}
}
